<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 用栅格系统排版 -->
      <el-row :gutter="50">
        <el-col :span="10">
          <el-input
            placeholder="请输入用户"
            v-model="queryUsers.query"
            clearable
            @clear="getqueryUsers"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getqueryUsers"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="showDialogo">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格区域 -->
      <user-table :userList="userList" />
      <!-- 分页区 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryUsers.pagenum"
        :page-sizes="[1, 2, 3, 4]"
        :page-size="queryUsers.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
      <!-- 添加用户对话框 -->
      <user-add-dialogo ref="dialogo" />
    </el-card>
  </div>
</template>

<script>
import UserAddDialogo from './usersChild/UserAddDialogo.vue'
import UserTable from './usersChild/UserTable.vue'
export default {
  components: { UserTable, UserAddDialogo },
  name: 'Users',
  data() {
    return {
      queryUsers: {
        query: '',
        //当前页码
        pagenum: 1,
        pagesize: 2,
      },
      userList: [],
      total: 0,
    }
  },
  created() {
    this.getqueryUsers()
  },
  methods: {
    //获取所需用户数据
    async getqueryUsers() {
      const { data: res } = await this.$http.get('users', {
        params: this.queryUsers,
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取失败')
      } else {
        this.userList = res.data.users
        this.total = res.data.total
      }
      // console.log(res)
    },
    //每页数据
    handleSizeChange(newSize) {
      // console.log(newSize)
      this.queryUsers.pagesize = newSize
      this.getqueryUsers()
    },
    //每页页码
    handleCurrentChange(newPage) {
      // console.log(newPage)
      this.queryUsers.pagenum = newPage
      this.getqueryUsers()
    },
    //显示对话框
    showDialogo() {
      // console.log(this)
      this.$refs.dialogo.dialogVisible = true
    },
  },
}
</script>

<style lang="less" scoped>
</style>